<?php

namespace polymerize\auth\module\login;

use app\common\InstanceTrait;
use app\common\LoginType;
use app\model\User as UserModel;
use app\model\UserBehaviorLog;
use app\model\UserToken;

class TouristLogin
{
    use InstanceTrait;

    /**
     * 游客登录
     *
     * @param string $uuid
     * @return array
     */
    public function touristLogin(string $uuid): array
    {
        try {
            $user = (new UserModel)->touristLogin($uuid);
            if ($user['code'] != 200) return ['code' => 400, 'msg' => $user['msg']];
            //是否是新用户 1是
            $data['is_new_user'] = isset($user['is_new_user']) ? $user['is_new_user'] : 0;
            $user                = $user['data'];

            $param = [
                'uid'         => $user['id'],
                'type'        => 2,
                'user_status' => $user['status'],
                'status'      => $user['status'],
                'login_type'  => LoginType::LOGIN_TYPE_TOURIST->value
            ];
            UserBehaviorLog::log($param);

            $token         = UserToken::loginToken($user, LoginType::LOGIN_TYPE_APPLE->value);
            $data['token'] = $token['token'] ?? '';

            //会员过期状态，1正常 2过期
            $data['is_vip'] = 2;
            $data['uid']    = $user['id'];

            return ['code' => 200, 'msg' => '登录成功', 'data' => $data];
        } catch (\Throwable $exception) {
            return ['code' => 400, 'msg' => '登录失败'];
        }
    }
}
